package com.ulic.customerManApplication.studentmodule.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseDao;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.pager.PagerModel;
import com.ulic.customerManApplication.studentmodule.bo.Student;
import com.ulic.customerManApplication.studentmodule.dao.StudentDao;
import com.ulic.customerManApplication.studentmodule.vo.StudentVO;
import com.ulic.customerManApplication.usermodule.bo.User;
@Component("studentDao")
public class StudentDaoImpl extends BaseDao implements StudentDao {

	Session session = null;
	@Override
	public void deleteStudent(Student student) throws Exception {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.delete(student);
		session.flush();
	}

	@Override
	public Student getStudent(int id) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Student.class);
		List list = cri.add(Restrictions.eq("id", id)).list();
		Student student = null;
		if(list.size()>0){
			student = (Student)list.get(0);
		}
		return student;
	}


	@Override
	public void insertStudent(Student student) throws Exception {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		try{
			session.save(student);
		}catch (Exception e) {
			e.printStackTrace();
		}
		session.flush();
	}

	@Override
	public void insertStudents(List<Student> students) throws Exception {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		for(Student student:students){
			session.save(student);
		}
		session.flush();
	}

	@Override
	public void updateStudent(Student student) throws Exception {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.saveOrUpdate(student);
		session.flush();
	}

	@Override
	public PagerModel getStudents(int curPage) {
		String sql = " from Student student order by createDate desc";
		return searchPaginated(sql, curPage, CONSTANT.PageSize);
	}

	@Override
	public PagerModel getStudents(StudentVO studentVO, int curPage) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from Student student where 1=1 ");
		Student student = studentVO.getStudent();
		if(student != null){
			if(student.getName() != null && student.getName().length() !=0){
				sb.append(" and student.name  like '%"+student.getName().trim()+"%'");
			}
			if(student.getGender()!=null &&! "No".equals(student.getGender())){
				sb.append(" and student.gender ='"+student.getGender()+"'");
			}
			if(student.getSchool()!= null && student.getSchool().length() != 0){
				sb.append(" and student.school like '%"+student.getSchool().trim()+"%'");
			}
			if(student.getSubCompany()!= null&&! "No".equals(student.getSubCompany())){
				sb.append(" and student.subCompany = '"+student.getSubCompany()+"'");
			}
			if(student.getSubCity()!= null&&! "No".equals(student.getSubCity())){
				sb.append(" and student.subCity = '"+student.getSubCity()+"'");
			}
			if(studentVO.getStartcreate()!=null &&studentVO.getStartcreate().length()!=0){
				sb.append(" and student.createDate>= to_date('"+studentVO.getStartcreate()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(studentVO.getEndcreate()!=null &&studentVO.getEndcreate().length()!=0){
				sb.append(" and student.createDate <= to_date('"+studentVO.getEndcreate()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
		}
		sb.append(" order by createDate desc");
		return searchPaginated(sb.toString(), curPage, CONSTANT.PageSize);
	}

	@Override
	public List<Student> getStudents(User user) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Student.class);
		List list = cri.createAlias("user", "u").add(Restrictions.eq("u.um_user_id", user.getUm_user_id())).list();
		return list;
	}

	@Override
	public PagerModel getStudents(StudentVO studentVO, int curPage, int totalNum) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from Student student where 1=1 ");
		Student student = studentVO.getStudent();
		if(student != null){
			if(student.getName() != null && student.getName().length() !=0){
				sb.append(" and student.name  like '%"+student.getName().trim()+"%'");
			}
			if(student.getGender()!=null &&! "No".equals(student.getGender())){
				sb.append(" and student.gender ='"+student.getGender()+"'");
			}
			if(student.getSchool()!= null && student.getSchool().length() != 0){
				sb.append(" and student.school like '%"+student.getSchool().trim()+"%'");
			}
			if(student.getSubCompany()!= null&&! "No".equals(student.getSubCompany())){
				sb.append(" and student.subCompany = '"+student.getSubCompany()+"'");
			}
			if(student.getSubCity()!= null&&! "No".equals(student.getSubCity())){
				sb.append(" and student.subCity = '"+student.getSubCity()+"'");
			}
			if(studentVO.getStartcreate()!=null &&studentVO.getStartcreate().length()!=0){
				sb.append(" and student.createDate>= to_date('"+studentVO.getStartcreate()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(studentVO.getEndcreate()!=null &&studentVO.getEndcreate().length()!=0){
				sb.append(" and student.createDate <= to_date('"+studentVO.getEndcreate()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
		}
		sb.append(" order by createDate desc");
		return searchPaginated(sb.toString(), curPage, totalNum);
	}

}
